import {createWebHistory, createRouter} from "vue-router";
import {CommentOutlined, ClockCircleOutlined} from "@ant-design/icons-vue";
import {handleGetRouter, navPath} from "./config";
import type {RouterData} from "./config";
import {STORAGE} from "@/utils/config";

// 单层路由页面
const Home = () => import("../pages/Home/Home.vue"); // 主页面
const Login = () => import("../pages/Login/Login.vue"); // 登录页面
//首页
const HomeRouter = () => import("../pages/HomeRouter/Home.vue"); // 主页面

// 系统设置
const User = () => import("../pages/Setting/User.vue"); //系统设置-员工账号
const Role = () => import("../pages/Setting/Role.vue"); // 系统设置-角色权限
const Log = () => import("../pages/Setting/Log.vue"); // 系统设置-操作日志
const WeChat = () => import("../pages/Setting/WeChat.vue"); // 系统设置-托管企业微信
//微信聊天室
const ChatRoom = () => import("../pages/WeChat/Online.vue"); //在线聊天室
const Material = () => import("../pages/WeChat/Material.vue"); //聊天素材库
const VerbalTrick = () => import("../pages/WeChat/VerbalTrick.vue"); //聊天室话术库
//数据统计
const ChatMessage = () => import("../pages/Statistics/ChatMessage.vue"); //聊天信息统计
const ChatCustomer = () => import("../pages/Statistics/CustomerInfo.vue"); //客户信息统计

// 聊天室
const WeChatWrapper = () => import("../pages/Online/Wrapper.vue"); // 聊天室-容器
const WeChatOnline = () => import("../pages/Online/Online.vue"); // 聊天室-在线聊天
const weChatHistory = () => import("../pages/Online/History.vue"); // 聊天室-历史记录

export const systemRouter: RouterData[] = [
    {title: "角色权限", path: navPath.role, component: Role},
    {title: "员工账号", path: navPath.user, component: User},
    {title: "操作日志", path: navPath.log, component: Log},
    {title: "托管企微", path: navPath.weChat, component: WeChat},
];

export const chatRoomRouter: RouterData[] = [
    {title: "在线聊天", path: navPath.chatRoom, component: ""},
    {title: "话术库", path: navPath.chatVerbalTrick, component: VerbalTrick},
    {title: "素材库", path: navPath.chatMaterial, component: Material},
];

export const statisticsRouter: RouterData[] = [
    {title: "聊天信息", path: navPath.chatMessage, component: ChatMessage},
    {title: "客户信息", path: navPath.customerInfo, component: ChatCustomer},
];

export const weChatRouter = [
    {path: navPath.weChatOnLine, component: WeChatOnline, title: "在线聊天", icon: CommentOutlined},
    {path: navPath.weChatHistory, component: weChatHistory, title: "历史记录", icon: ClockCircleOutlined},
];
// 主页路由
export const homeRouter = [{title: "首页", path: navPath.homeRouter, component: HomeRouter}];

const childrenRouter: any[] = handleGetRouter([...systemRouter, ...chatRoomRouter, ...statisticsRouter, ...homeRouter]); // 系统设置路由
const weChatChildrenRouter: any[] = handleGetRouter(weChatRouter); // 聊天室路由

const routes: any[] = [
    //{path: '/', redirect: {name: '/login'}},
    {path: "/login", component: Login},
    {path: "/weChat", component: WeChatWrapper, children: weChatChildrenRouter},
    {path: "/home", component: Home, children: childrenRouter},
];

const router = createRouter({
    history: createWebHistory(),
    routes,
});

router.beforeEach(to => {
    const auth = sessionStorage.getItem(STORAGE.AUTHORIZATION) || "";
    if (!auth) {
        if (to.path !== "/login") {
            return "/login";
        }
    }
    return true;
});

export default router;
